/**
 * Created by nxs@winning.com.cn on 18/6/26.
 */

var timer;
var disFlag = false;
$(document).ready(function () {
    logoutbz(); //是否退出确认
    queryAppByYydm();//查询应用信息展示名称
    selectSylb(); //确认首页类别
    querySubMenu();
    getMsgCount();//获取通知条数
    init();


});

/**
 * 初始化操作
 */
function  init() {
    $(".navbar-minimalize").click(function () {
        //显示隐藏log.png
        $(".navbar-brand").toggle();
        $("body").toggleClass("mini-navbar");
        if( $("body").hasClass("mini-navbar")){
            $(".secondIconA").siblings("span").hide();
        }else{
            $(".secondIconA").siblings("span").show();
        }
            SmoothlyMenu();
    })
}

/**
 * 确认首页类别
 */
function  selectSylb() {
    var jgbm="";
    var jglb = "";
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        url: 'commonapi/jgbm',
        success: function (jsonData) {
            jgbm = jsonData.jgbm;
            jglb = jsonData.jglb;
            localStorage.setItem("yhJgmc", jsonData.jgmc);
            //edit by dangbo 2021年1月21日19:07:02 查询业务场景参数，属于区域还是院内
            $.ajax({
                type: "GET",
                contentType: 'application/json',
                dataType: "json",
                cache: false,
                async: false,
                url: "commonapi/queryQjcs/" + 'sjzk_ssywcj',
                success: function (jsonData) {
                    if (jsonData.code == 200) {
                      $('.helpCenter').hide();
                     if (jsonData.qjcsEntity == undefined || jsonData.qjcsEntity ==null){
                        localStorage.setItem("ssywcj", 'AREA');
                        }

                       if (jsonData.qjcsEntity != undefined && jsonData.qjcsEntity !=null && jsonData.qjcsEntity.csz == 'HOSP'){
                          localStorage.setItem("ssywcj", 'HOSP');
                            $('.helpCenter').show();
                            var ss=$('#iframeSy');
                            $('#iframeSy1').empty();
                            $('#iframeSy2').empty();
                            var str1=' <a href="javascript:;" class="active J_menuTab" data-id="dqmt-zkztjk.html">质控运维看板</a>';
                            var str2='<iframe id ="iframeSy" class="J_iframe" style="background-color: #f5f8f9" name="iframe0" ' +
                                'width="100%" height="100%"   src="dqmt-zkztjk.html" frameborder="0" ' +
                                'data-id="dqmt-zkztjk.html" seamless></iframe>';
                            $('#iframeSy1').append(str1);
                            $('#iframeSy2').append(str2);
                        } else  if (jsonData.qjcsEntity != undefined && jsonData.qjcsEntity !=null && jsonData.qjcsEntity.csz == 'NHZK'){
                        $('.helpCenter').show();
                        var ss=$('#iframeSy');
                        $('#iframeSy1').empty();
                        $('#iframeSy2').empty();
                        var str1=' <a href="javascript:;" class="active J_menuTab" data-id="dqmt-zlpjkb.html">质控评价看板</a>';
                        var str2='<iframe id ="iframeSy" class="J_iframe" style="background-color: #f5f8f9" name="iframe0" ' +
                            'width="100%" height="100%"   src="dqmt-zlpjkb.html" frameborder="0" ' +
                            'data-id="dqmt-zlpjkb.html" seamless></iframe>';
                        $('#iframeSy1').append(str1);
                        $('#iframeSy2').append(str2);
                      }
                       else if (jglb !='03'){
                          $.ajax({
                              type: "GET",
                              contentType: 'application/json',
                              dataType: "json",
                              cache: false,
                              async: false,
                              url: "commonapi/queryQjcs/" + 'sjzk_ptJgbm',
                              success: function (jsonData) {
                                  if (jsonData.code == 200) {
                                      if (jsonData.qjcsEntity != undefined && jsonData.qjcsEntity !=null && jsonData.qjcsEntity.csz != jgbm){
                                          var ss=$('#iframeSy');
                                          //$('#iframeSy1').html='';
                                          $('#iframeSy1').empty();
                                          $('#iframeSy2').empty();
                                          var str1=' <a href="javascript:;" class="active J_menuTab" data-id="dqmt-yyZlfx.html">首页</a>';
                                          var str2='<iframe id ="iframeSy" class="J_iframe" style="background-color: #f5f8f9" name="iframe0" ' +
                                              'width="100%" height="100%"   src="dqmt-yyZlfx.html" frameborder="0" ' +
                                              'data-id="dqmt-yyZlfx.html" seamless></iframe>';


                                          $('#iframeSy1').append(str1);
                                          $('#iframeSy2').append(str2);



                                      }

                                  }

                              }
                          });
                      }
                        if(disFlag && jglb !='03'){
                            $('#iframeSy1').empty();
                            $('#iframeSy2').empty();
                            var str2='<iframe id ="iframeSy" class="J_iframe" style="background-color: #f5f8f9" name="iframe0" ' +
                                'width="100%" height="100%"   src="dqmt-zlzl.html" frameborder="0" ' +
                                'data-id="dqmt-zlzl.html" seamless></iframe>';
                            $('#iframeSy2').append(str2);
                            menuClink('dqmt-zlzl.html?mcode=910202&cdType=third',null,'前置机质控');

                        }


                    }

                }
            });


        }
    });


}




/**
 * 退出按钮操作
 */
function logout() {
    LayerWn.showConfirm("确定要退出吗？", function () {
        resetToLogin();
    });
}



function getMsgCount() {
    $("#goYcjk").click(function () {
        menuClink("dqmt-ycjk.html?mcode=910701&cdType=third",null,'异常监控');
    });

    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        url: "ycjk/getUnHandleCount",
        success: function (jsonData) {
            if (jsonData.code == 200) {   //保存成功
                var count = jsonData.count;
                if(count){
                    $("#total-count").html(count);
                }
            }
        }
    });
}

/**
 *  1 显示  0  隐藏
 */
function logoutbz() {
    $("#loginUser").text(localStorage.getItem("userName"));
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        cache: false,
        async: false,
        url: "commonapi/queryQjcs/" + 'sjzk_ptJgbm',
        success: function (jsonData) {
            if (jsonData.code == 200) {
                 if (jsonData.qjcsEntity != undefined && jsonData.qjcsEntity !=null && jsonData.qjcsEntity.csz == "0")
                    $('.logout').hide();
                else
                    $('.logout').show();
            }
        }
    });
}

/**
 * 查询应用信息展示名称
 */
function queryAppByYydm() {
    var yydm = "DS";   //应用代码
    if (yydm) {
        $.ajax({
            type: "GET",
            dataType: "json",
            // url: "application/queryApp/" + yydm,
            url: "application/queryApp/"+ yydm,
            success: function (jsonData) {
                if (jsonData.code == 200) {   //查询成功
                    // $('#appForm').populateForm(.app);
                    var zsmc = jsonData.app.zsmc;
                    //alert("zsmc:" + zsmc);
                    if( $.trim(zsmc)!=""){
                        $("#zsmc").html($.trim(zsmc));
                        $("#title").html($.trim(zsmc));
                    }
                } else {

                }
            }
        });
    }
}

    /**
     * 退出按钮操作
     */
    function logout() {
        LayerWn.showConfirm("确定要退出吗？", function () {
            resetToLogin();
        });
    }

function resetToLogin() {

    var username = localStorage.getItem("username");
    localStorage.removeItem("userName");
    //删除本地token
    sessionStorage.removeItem("token");
    $.ajax({
        type: "GET",
        dataType: "json",
        contentType: 'application/json',
        url: "logout/"+username
    });
    //跳转到登录页面
    location.href = baseURL + 'login.html';
}



function second_onclick(param, obj, module) {
    /*  $(obj).siblings().css("background-color","#FFF");*/
    $(obj).siblings().find("li a").css("background-color", "#FFF");
    $(".secondIconA").show();
    $(".secondIconB").hide();
    $("#" + param + "A").hide();
    $("#" + param + "B").show();
    if (!$('body').hasClass('mini-navbar'))
        $("body").removeClass("mini-navbar");
    else {
        $("body").toggleClass("mini-navbar");
        SmoothlyMenu();
    }
    if(module != null && module != '' && module.toLowerCase() != 'null' && $(obj).hasClass('active')){
     //   menuClink(module, null, mcaption);
        $(obj).find("a").first().attr("href",module);
        $(obj).find("a").first().addClass("SecondClick");
        $(".SecondClick").on("click", c);
        $(obj).find("a").first().click();
    }
}

/**
 * 一级菜单鼠标移入菜单操作
 */
function menuMousemoveF(obj) {
    $(obj).children(".firstIconB").show();
    $(obj).children(".firstIconA").hide();
}

/**
 * 二级菜单鼠标移入菜单操作
 */
function menuMousemove(obj) {
    $(obj).children(".secondIconB").show();
    $(obj).children(".secondIconA").hide();
    if( $("body").hasClass("mini-navbar")) {
        $(obj).children(".secondIconA").siblings("span").show();
    }
}

/**
 * 一级菜单鼠标移出菜单操作
 */
function menuMouseoutF(obj) {
    $(obj).children(".firstIconA").show();
    $(obj).children(".firstIconB").hide();
}

/**
 * 二级菜单鼠标移入菜单操作
 */
function menuMouseout(obj) {
    $(obj).children(".secondIconB").hide();
    $(obj).children(".secondIconA").show();
    if( $("body").hasClass("mini-navbar")) {
        $(obj).children(".secondIconA").siblings("span").hide();
    }
}


//查询二三级菜单
function querySubMenu() {
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        url: "index/getGncdTree?applicationId=DQMT",
        success: function (jsonData) {
            if(jsonData.code == 200){
                //如果只显示前置机质控和项目自检则隐藏首页
                if(jsonData.tree){
                    var list = jsonData.tree[0].thirdlist;
                    if(list){
                        if(JSON.stringify(list).indexOf('910201')>-1&&JSON.stringify(list).indexOf('910202')>-1){
                            disFlag=true;
                           /* if(disFlag){
                                $('#iframeSy1').empty();
                                $('#iframeSy2').empty();
                                var str2='<iframe id ="iframeSy" class="J_iframe" style="background-color: #f5f8f9" name="iframe0" ' +
                                    'width="100%" height="100%"   src="dqmt-zlzl.html" frameborder="0" ' +
                                    'data-id="dqmt-zlzl.html" seamless></iframe>';
                                $('#iframeSy2').append(str2);
                                menuClink('dqmt-zlzl.html?mcode=910202&cdType=third',null,'前置机质控');

                            }*/
                        }
                    }
                }

                createMenu(jsonData.tree);
                localStorage.setItem("gncdList",JSON.stringify(jsonData.tree));
            }
        },
        error: function (errorMsg) {
        }
    });
}



function createMenu(secondlist){
    var menuHtml = '';
    $.each(secondlist, function (index, entity) {
        var secondHtml = '';
        if (entity.thirdlist && entity.thirdlist.length >0) {
            secondHtml += '<li  style="margin:2px 0"  onclick="second_onclick(' + entity.mcode + ',this,\''+ entity.module + '\')">';
            secondHtml += '<a class="second-a" href="#" onmousemove="menuMousemove(this)" onmouseout="menuMouseout(this)">';
            secondHtml += '<img class="secondIconA" id="' + entity.mcode + 'A" src="img/menu/' + entity.mcode + '.png" style="margin-right:5px;margin-bottom: 4px;display: inline"></img>';
            secondHtml += '<img class="secondIconB" id="' + entity.mcode + 'B" src="img/menu/' + entity.mcode + '.png" style="display:none;margin-right:5px;margin-bottom: 4px"></img>';
            secondHtml += '<span class="nav-label">' + entity.mcaption + '</span>';
            // secondHtml += '<span class="fa arrow"></span>';
            secondHtml += '</a>';
            if (null != entity.thirdlist) {
                var thirdHtml = '';
                if (entity.thirdlist.length > 0) {
                    secondHtml += '<ul class="nav-second-wn nav-second-level-wn">';
                    $.each(entity.thirdlist, function (index, thirdEntity) {
                        thirdHtml += '<li>';
                        thirdHtml += '<a class="J_menuItem" href=' + thirdEntity.module + '?mcode=' + thirdEntity.mcode + '&cdType=third>' + thirdEntity.mcaption + '</a>';
                        thirdHtml += '</li>';
                    });
                    secondHtml += thirdHtml;
                    secondHtml += '</ul>';
                }
            }
            secondHtml += '</li>';
        } else {
            secondHtml += '<li>';
            secondHtml += '<a class="J_menuItem" href="'+ entity.module +'" onmousemove="menuMousemove(this)" onmouseout="menuMouseout(this)">';
            secondHtml += '<img class="secondIconA" id="' + entity.mcode + 'A" src="img/menu/' + entity.mcode + '.png" style="margin-right:5px;margin-bottom: 4px;display: inline"></img>';
            secondHtml += '<img class="secondIconB" id="' + entity.mcode + 'B" src="img/menu/' + entity.mcode + '.png" style="display:none;margin-right:5px;margin-bottom: 4px"></img>';
            secondHtml += '<span class="nav-label">' + entity.mcaption + '</span>';
            secondHtml += '</a>';
            secondHtml += '</li>';
        }
        menuHtml += secondHtml;
    });

    $('#side-menu').html(menuHtml);
    $("#side-menu").metisMenu();
    $(".J_menuItem").on("click", c);
}

function c() {
    var o = $(this).attr("href"),
        m = $(this).data("index"),
        l = $.trim($(this).text()),
        k = true;
    if (o == undefined || $.trim(o).length == 0) {
        return false
    }
    $(".J_menuTab").each(function () {
        if ($(this).data("id").indexOf(o)>-1) {
            if (!$(this).hasClass("active")) {
                $(this).addClass("active").siblings(".J_menuTab").removeClass("active");
                g(this);
                $(".J_mainContent .J_iframe").each(function () {
                    if ($(this).data("id").indexOf(o)>-1) {
                        $(this).show().siblings(".J_iframe").hide();
                        return false
                    }
                })
            }
            k = false;
            return false
        }
    });
    if (k) {
        /*edit by dangbo 2021年5月13日00:30:44 修改导航栏关闭×按钮的样式*/
        var p = '<a href="javascript:;" class="active J_menuTab" data-id="' + o + '">' + l + ' <i class="fa fa-remove"></i></a>';
        $(".J_menuTab").removeClass("active");
        var n = '<iframe class="J_iframe" name="iframe' + m + '" width="100%" height="100%" src="' + o + '" frameborder="0" data-id="' + o + '" seamless></iframe>';
        $(".J_mainContent").find("iframe.J_iframe").hide().parents(".J_mainContent").append(n);
        $(".J_menuTabs .page-tabs-content").append(p);
        g($(".J_menuTab.active"))
    }
    $(".nav-second-wn").find("li").removeClass("active");
    $(this).parent().addClass("active");
    return false
}

function f(l) {
    var k = 0;
    $(l).each(function () {
        k += $(this).outerWidth(true)
    });
    return k
}
function g(n) {
    var o = f($(n).prevAll()),
        q = f($(n).nextAll());
    var l = f($(".content-tabs").children().not(".J_menuTabs"));
    var k = $(".content-tabs").outerWidth(true) - l;
    var p = 0;
    if ($(".page-tabs-content").outerWidth() < k) {
        p = 0
    } else {
        if (q <= (k - $(n).outerWidth(true) - $(n).next().outerWidth(true))) {
            if ((k - $(n).next().outerWidth(true)) > q) {
                p = o;
                var m = n;
                while ((p - $(m).outerWidth()) > ($(".page-tabs-content").outerWidth() - k)) {
                    p -= $(m).prev().outerWidth();
                    m = $(m).prev()
                }
            }
        } else {
            if (o > (k - $(n).outerWidth(true) - $(n).prev().outerWidth(true))) {
                p = o - $(n).prev().outerWidth(true)
            }
        }
    }
    $(".page-tabs-content").animate({
        marginLeft: 0 - p + "px"
    }, "fast")

}
